<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>main (trafmonitor.rb)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File trafmonitor.rb, line 25</span>
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">main</span>
    <span class="ruby-identifier">pidfile</span>   = <span class="ruby-identifier">$config</span>.<span class="ruby-identifier">pidfile</span>
    <span class="ruby-identifier">filterexp</span> = <span class="ruby-identifier">$config</span>.<span class="ruby-identifier">filter</span>
    <span class="ruby-identifier">stats</span>     = <span class="ruby-constant">Stats</span>.<span class="ruby-identifier">new</span>
    <span class="ruby-identifier">sniffer</span>   = <span class="ruby-constant">Pcaplet</span>.<span class="ruby-identifier">new</span>( <span class="ruby-value str">'-i eth0 -s 1500'</span> )
    <span class="ruby-identifier">my_filter</span> = <span class="ruby-constant">Pcap</span><span class="ruby-operator">::</span><span class="ruby-constant">Filter</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">filterexp</span>, <span class="ruby-identifier">sniffer</span>.<span class="ruby-identifier">capture</span> )
    
    <span class="ruby-identifier">pid_handler</span>( <span class="ruby-identifier">pidfile</span> )
    <span class="ruby-identifier">sniffer</span>.<span class="ruby-identifier">add_filter</span>( <span class="ruby-identifier">my_filter</span> )

    <span class="ruby-identifier">$LOG</span>.<span class="ruby-identifier">debug</span> <span class="ruby-keyword kw">do</span> <span class="ruby-value str">'Starting packet sniffer loop.'</span> <span class="ruby-keyword kw">end</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">$DEBUG</span>
    
    <span class="ruby-identifier">sniffer</span>.<span class="ruby-identifier">each_packet</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">p</span><span class="ruby-operator">|</span>
                <span class="ruby-identifier">stats</span>.<span class="ruby-identifier">account</span>( <span class="ruby-identifier">p</span>.<span class="ruby-identifier">ip_dst</span>.<span class="ruby-identifier">to_num_s</span>, <span class="ruby-identifier">p</span>.<span class="ruby-identifier">size</span>.<span class="ruby-identifier">to_i</span> )
    
        <span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>( <span class="ruby-value str">&quot;USR1&quot;</span> ) <span class="ruby-keyword kw">do</span>
            <span class="ruby-identifier">$LOG</span>.<span class="ruby-identifier">debug</span> <span class="ruby-keyword kw">do</span> <span class="ruby-value str">'Stats dumped with SIGUSR1'</span> <span class="ruby-keyword kw">end</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">$DEBUG</span>
            <span class="ruby-identifier">stats</span>.<span class="ruby-identifier">to_log</span>
        <span class="ruby-keyword kw">end</span>
    
        <span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>( <span class="ruby-value str">&quot;USR2&quot;</span> ) <span class="ruby-keyword kw">do</span>
            <span class="ruby-identifier">$LOG</span>.<span class="ruby-identifier">debug</span> <span class="ruby-keyword kw">do</span> <span class="ruby-value str">'Stats cleared with SIGUSR2'</span> <span class="ruby-keyword kw">end</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">$DEBUG</span>
            <span class="ruby-identifier">stats</span>.<span class="ruby-identifier">reset</span>
        <span class="ruby-keyword kw">end</span>

        <span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>( <span class="ruby-value str">&quot;INT&quot;</span> ) <span class="ruby-keyword kw">do</span>
            <span class="ruby-identifier">$LOG</span>.<span class="ruby-identifier">info</span> <span class="ruby-keyword kw">do</span> <span class="ruby-value str">'Caught interrupt. Dumping stats and terminating.'</span> <span class="ruby-keyword kw">end</span>
            <span class="ruby-identifier">stats</span>.<span class="ruby-identifier">to_log</span>
            <span class="ruby-keyword kw">begin</span>
                <span class="ruby-constant">File</span>.<span class="ruby-identifier">delete</span>( <span class="ruby-identifier">$config</span>.<span class="ruby-identifier">pidfile</span> )
            <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">SystemCallError</span>
            <span class="ruby-keyword kw">end</span>
            <span class="ruby-identifier">exit</span> <span class="ruby-value">0</span>
        <span class="ruby-keyword kw">end</span>
    }
<span class="ruby-keyword kw">end</span></pre>
</body>
</html>